package com.maylann.ratiocheater.controller;

import com.maylann.ratiocheater.model.Connection;
import com.maylann.ratiocheater.model.Task;
import com.maylann.ratiocheater.model.Torrent;
import com.maylann.ratiocheater.transform.TransformationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * by Maylann
 */

@Controller
public class TaskController {

    private static final int PORT = 50000;
    private static final String CLIENT_VERSION = "UT1610";
    private static final String USER_AGENT = "uTorrent/1610";

    private FileController fileController;
    private List<TaskRunner> threadList;

    @Autowired
    public TaskController(FileController fileController) {
        this.fileController = fileController;
        this.threadList = new ArrayList<TaskRunner>();
    }

    public Task createTask(long speed, String torrentFilename) throws IOException, TransformationException {
        Torrent torrent = fileController.openTorrentFile(torrentFilename);
        Connection connection = new Connection(PORT, CLIENT_VERSION, USER_AGENT, torrent.getTracker().getUrl());
        return new Task(torrent, speed, connection);
    }

    public void startTask(Task task) {
        TaskRunner taskRunner = new TaskRunner(task);
        threadList.add(taskRunner);
        taskRunner.run();
    }

    public void stopTask(Task task) {

    }

}
